package com.zzs.judge.dao.redis;

import com.zzs.common.core.constant.RedisKeyConsts;
import com.zzs.common.core.entity.mysql.TestCaseDO;
import com.zzs.common.core.entity.redis.TestCaseCache;
import com.zzs.common.core.enums.ResponseCode;
import com.zzs.common.redis.service.RedisService;
import com.zzs.common.security.exception.ServiceException;
import com.zzs.judge.dao.mysql.TestCaseMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @author zzs
 * @date 2025/03/31
 */
@Repository
public class TestCaseCacheManager {
    @Autowired
    private TestCaseMapper testCaseMapper;
    @Autowired
    private RedisService redisService;

    public TestCaseCache get(long problemId) {
        String key = RedisKeyConsts.getTestCaseKey(problemId);
        TestCaseCache value = redisService.getValue(key, TestCaseCache.class);
        if (value == null) {
            TestCaseDO testCaseDO = testCaseMapper.get(problemId);
            if (testCaseDO == null) {
                throw new ServiceException(ResponseCode.NOT_FOUND, "测试用例不存在, 题目id: " + problemId);
            }
            value = new TestCaseCache(problemId, testCaseDO.getTestCase());
        }
        return value;
    }
}
